/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IOEditorPanel.java
 *
 * Created on Jan 4, 2010, 12:36:41 PM
 */
package dae.fxcreator.node.graph;

import dae.fxcreator.io.templates.NodeTemplateLibrary;
import dae.fxcreator.node.Semantic;
import dae.fxcreator.node.ShaderStruct;
import dae.fxcreator.node.ShaderType;
import dae.fxcreator.node.graph.model.IONodeInputTableModel;
import dae.fxcreator.node.graph.model.IONodeInputTableModel.STATE;
import dae.fxcreator.node.graph.model.IOTableModel;
import dae.fxcreator.node.graph.model.SemanticCellEditor;
import dae.fxcreator.node.graph.model.ShaderTypeCellEditor;
import dae.fxcreator.node.graph.model.StructTableModel;
import org.jdesktop.swingx.JXPanel;

/**
 *
 * @author Koen
 */
public class IOEditorPanel extends JXPanel implements IONodeModelListener {

    private JGraphNode model;
    private NodeTemplateLibrary library;

    /** Creates new form IOEditorPanel */
    public IOEditorPanel() {
        initComponents();
        setIONode(null);
       
    }

    public void setNodeTemplateLibrary(NodeTemplateLibrary library){
        this.library = library;

        ShaderTypeCellEditor editor = new ShaderTypeCellEditor(library);
        tblInputs.setDefaultEditor(ShaderType.class, editor);
        tblOutputs.setDefaultEditor(ShaderType.class, editor);

        SemanticCellEditor semanticEditor = new SemanticCellEditor();
        tblInputs.setDefaultEditor(Semantic.class, semanticEditor);
        tblOutputs.setDefaultEditor(Semantic.class, semanticEditor);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tblInputsPanel = new org.jdesktop.swingx.JXPanel();
        btnDeleteInput = new javax.swing.JButton();
        scrTblInputs = new javax.swing.JScrollPane();
        tblInputs = new org.jdesktop.swingx.JXTable();
        lblInputStruct = new javax.swing.JLabel();
        txtInputStruct = new javax.swing.JTextField();
        btnAddInput = new javax.swing.JButton();
        tblOutputsPanel = new org.jdesktop.swingx.JXPanel();
        btnAddOutput = new javax.swing.JButton();
        btnDeleteOutput = new javax.swing.JButton();
        scrTblOutputs = new javax.swing.JScrollPane();
        tblOutputs = new org.jdesktop.swingx.JXTable();
        lblOutputStruct = new javax.swing.JLabel();
        txtOutputStruct = new javax.swing.JTextField();

        setLayout(new java.awt.GridLayout(2, 0));

        tblInputsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Inputs"));

        btnDeleteInput.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dae/images/destroyaction.png"))); // NOI18N
        btnDeleteInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteInputActionPerformed(evt);
            }
        });

        tblInputs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblInputs.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblInputs.setPreferredScrollableViewportSize(new java.awt.Dimension(200, 100));
        tblInputs.setPreferredSize(new java.awt.Dimension(200, 100));
        scrTblInputs.setViewportView(tblInputs);

        lblInputStruct.setText("Input Struct : ");

        btnAddInput.setText("+");
        btnAddInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddInputActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout tblInputsPanelLayout = new javax.swing.GroupLayout(tblInputsPanel);
        tblInputsPanel.setLayout(tblInputsPanelLayout);
        tblInputsPanelLayout.setHorizontalGroup(
            tblInputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tblInputsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tblInputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrTblInputs, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tblInputsPanelLayout.createSequentialGroup()
                        .addComponent(btnDeleteInput)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAddInput))
                    .addGroup(tblInputsPanelLayout.createSequentialGroup()
                        .addComponent(lblInputStruct)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtInputStruct, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)))
                .addContainerGap())
        );
        tblInputsPanelLayout.setVerticalGroup(
            tblInputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tblInputsPanelLayout.createSequentialGroup()
                .addGroup(tblInputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblInputStruct)
                    .addComponent(txtInputStruct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrTblInputs, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tblInputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnAddInput)
                    .addComponent(btnDeleteInput))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(tblInputsPanel);

        tblOutputsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Outputs"));

        btnAddOutput.setText("+");
        btnAddOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddOutputActionPerformed(evt);
            }
        });

        btnDeleteOutput.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dae/images/destroyaction.png"))); // NOI18N
        btnDeleteOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteOutputActionPerformed(evt);
            }
        });

        tblOutputs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblOutputs.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblOutputs.setPreferredScrollableViewportSize(new java.awt.Dimension(200, 100));
        tblOutputs.setPreferredSize(new java.awt.Dimension(200, 100));
        scrTblOutputs.setViewportView(tblOutputs);

        lblOutputStruct.setText("Output Struct : ");

        javax.swing.GroupLayout tblOutputsPanelLayout = new javax.swing.GroupLayout(tblOutputsPanel);
        tblOutputsPanel.setLayout(tblOutputsPanelLayout);
        tblOutputsPanelLayout.setHorizontalGroup(
            tblOutputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tblOutputsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tblOutputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrTblOutputs, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, tblOutputsPanelLayout.createSequentialGroup()
                        .addComponent(lblOutputStruct)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtOutputStruct, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                    .addGroup(tblOutputsPanelLayout.createSequentialGroup()
                        .addComponent(btnDeleteOutput)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAddOutput)))
                .addContainerGap())
        );
        tblOutputsPanelLayout.setVerticalGroup(
            tblOutputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tblOutputsPanelLayout.createSequentialGroup()
                .addGroup(tblOutputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOutputStruct)
                    .addComponent(txtOutputStruct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrTblOutputs, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tblOutputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnAddOutput)
                    .addComponent(btnDeleteOutput))
                .addContainerGap())
        );

        add(tblOutputsPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddInputActionPerformed

        IOTableModel tableModel = (IOTableModel) tblInputs.getModel();
        tableModel.add();
    }//GEN-LAST:event_btnAddInputActionPerformed

    private void btnDeleteInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteInputActionPerformed

        IOTableModel tableModel = (IOTableModel) tblInputs.getModel();
        int selectedIndex = tblInputs.getSelectedRow();
        if (selectedIndex > -1) {
            tableModel.delete(selectedIndex);
        }
    }//GEN-LAST:event_btnDeleteInputActionPerformed

    private void btnAddOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddOutputActionPerformed

        IOTableModel tableModel = (IOTableModel) tblOutputs.getModel();
        tableModel.add();
    }//GEN-LAST:event_btnAddOutputActionPerformed

    private void btnDeleteOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteOutputActionPerformed

        IOTableModel tableModel = (IOTableModel) tblOutputs.getModel();
        int selectedIndex = tblOutputs.getSelectedRow();
        if (selectedIndex > -1) {
            tableModel.delete(selectedIndex);
        }
    }//GEN-LAST:event_btnDeleteOutputActionPerformed

    public void setIONode(JGraphNode gnode) {
        this.model = gnode;

        // tblInputs.getModel().removeTableModelListener(this);
        //tblOutputs.getModel().removeTableModelListener(this);
        if (gnode != null) {
            if (gnode.getUserObject().isInputStructSet()) {
                ShaderStruct inputStruct = gnode.getUserObject().getInputStruct();
                lblInputStruct.setVisible(true);
                txtInputStruct.setVisible(true);
                txtInputStruct.setText(inputStruct.getId());
                StructTableModel tableModel = new StructTableModel(gnode,gnode.getUserObject().getInputStruct());
                tblInputs.setModel(tableModel);
            } else {
                IONodeInputTableModel inputTableModel = new IONodeInputTableModel(gnode, STATE.INPUT);
                tblInputs.setModel(inputTableModel);
                lblInputStruct.setVisible(false);
                txtInputStruct.setVisible(false);
            }

            if (gnode.getUserObject().isOutputStructSet()) {
                ShaderStruct outputStruct = gnode.getUserObject().getOutputStruct();
                lblOutputStruct.setVisible(true);
                txtOutputStruct.setVisible(true);
                txtOutputStruct.setText(outputStruct.getId());
                StructTableModel tableModel = new StructTableModel(gnode,gnode.getUserObject().getOutputStruct());
                tblOutputs.setModel(tableModel);
            }else{
                IONodeInputTableModel outputTableModel = new IONodeInputTableModel(gnode, STATE.OUTPUT);
                tblOutputs.setModel(outputTableModel);
                lblOutputStruct.setVisible(false);
                txtOutputStruct.setVisible(false);
            }
        } else {
            IONodeInputTableModel inputTableModel = new IONodeInputTableModel(null, STATE.INPUT);
            tblInputs.setModel(inputTableModel);

            IONodeInputTableModel outputTableModel = new IONodeInputTableModel(null, STATE.OUTPUT);
            tblOutputs.setModel(outputTableModel);
        }
        //tblInputs.getModel().addTableModelListener(this);
        //tblOutputs.getModel().addTableModelListener(this);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddInput;
    private javax.swing.JButton btnAddOutput;
    private javax.swing.JButton btnDeleteInput;
    private javax.swing.JButton btnDeleteOutput;
    private javax.swing.JLabel lblInputStruct;
    private javax.swing.JLabel lblOutputStruct;
    private javax.swing.JScrollPane scrTblInputs;
    private javax.swing.JScrollPane scrTblOutputs;
    private org.jdesktop.swingx.JXTable tblInputs;
    private org.jdesktop.swingx.JXPanel tblInputsPanel;
    private org.jdesktop.swingx.JXTable tblOutputs;
    private org.jdesktop.swingx.JXPanel tblOutputsPanel;
    private javax.swing.JTextField txtInputStruct;
    private javax.swing.JTextField txtOutputStruct;
    // End of variables declaration//GEN-END:variables
}
